package wnhz.com.business.changepwd;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.forget.ForgetPasswordActivity;
import wnhz.com.business.forget.ForgetPasswordResetActivity;
import wnhz.com.business.groupshop.bean.InfoBean;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.StatusBarUtil;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;

/**
 * Created by lizhenglong on 2017-10-27 0027.
 * 修改密码_获取验证码
 */

public class ChangePwd2Activity extends MyBaseActivity {

    @BindView(R.id.v_statusbar)
    View vStatusbar;
    @BindView(R.id.rl_register_back)//返回
            RelativeLayout rlRegisterBack;
    @BindView(R.id.tv_changepwd2_phone)//手机号码
            TextView tvChangepwd2Phone;
    @BindView(R.id.ed_changepwd2_code)//输入验证码
            EditText edChangepwd2Code;
    @BindView(R.id.tv_changepwd2_getcode)//获取验证码
            TextView tvChangepwd2Getcode;
    @BindView(R.id.btn_changepwd2)//下一步
            Button btnChangepwd2;

    private String phone = "";
    private String type = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chagepwd2);
        ButterKnife.bind(this);
        phone = getIntent().getStringExtra("phone");
        type = getIntent().getStringExtra("type");
        bindPhonegetcode();
        //状态栏设置
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.transparent), 0);
        StatusBarUtil.setDarkMode(this, true);
        StatusBarUtil.immersive(this, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
////            //透明导航栏
////            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
////            //改变状态栏颜色
////            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            vStatusbar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vStatusbar.getLayoutParams();
            params.height = statusHeight;
            vStatusbar.setLayoutParams(params);
        }

        tvChangepwd2Phone.setText(phone);

    }

    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what) {
            case 100:
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvChangepwd2Getcode, 60000, 1000);
                mCountDownTimerUtils.start();
                break;
        }
    }

    @OnClick({R.id.rl_register_back, R.id.tv_changepwd2_getcode, R.id.btn_changepwd2})
    void Click(View v) {
        switch (v.getId()) {
            case R.id.rl_register_back:
                finish();
                break;
            case R.id.tv_changepwd2_getcode:
                if (TextUtils.isEmpty(phone)) {
                    MyToast("请输入手机号码");
                    return;
                }
                if (!ConfirmUtils.isMobileNO(phone)) {
                    MyToast("手机号码格式不正确");
                    return;
                }
                bindPhonegetcode();
                break;
            case R.id.btn_changepwd2://下一步
                if (TextUtils.isEmpty(type)) {
                    MyToast("获取设置密码类型失败");
                    return;
                }
                if (TextUtils.isEmpty(edChangepwd2Code.getText().toString())) {
                    MyToast("请输入验证码");
                    return;
                }
                yanZhengMaVeri(phone,edChangepwd2Code.getText().toString());
                break;
        }
    }

    //    获取验证码
    //    @param Str    code      接口请求密码  UNOHACHA
    //    @param Int    mobile    手机号   （加密）
    //    @param Int    type      1 注册  2 登录  3 忘记密码   4 更换手机号  5 修改交易密码  6 修改登录密码  7三方登陆  8绑定银行卡
    //    syb.unohacha.com/Api/Api/User_getMessage
    private void bindPhonegetcode() {
        Map<String, String> map = new HashMap<>();
        map.put("code", Url.CODE);
        map.put("mobile", Base64Util.encodedString2(phone));
        map.put("type", type);
        Log.e("修改密码获取验证码", "" + Url.REGISTER_GETCODE);
        OkGo.<InfoBean>post(Url.REGISTER_GETCODE)
                .tag(this)
                .params(map, false)
                .execute(new DialogCallback<InfoBean>(this) {
                    @Override
                    public void onSuccess(Response<InfoBean> response) {
                        Log.e("REGISTER_GETCODE", "" + response);
                        if (response.body().getRe().equals("1")) {
                            handler.sendEmptyMessage(100);
                            MyToast(response.body().getInfo());
                        }else {
                            MyToast(response.body().getInfo());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<InfoBean> response) {
                        super.onError(response);
                    }
                });
    }
    private void yanZhengMaVeri(String mobile,String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", Prefer.getInstance().getToken());
        map.put("mobile", Base64Util.encodedString2(mobile));
        map.put("type", type);
        map.put("code", code);

        XUtil.Post(Url.USER_VERIFICATION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("==获取= ", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {
                        Intent intent = new Intent(ChangePwd2Activity.this, ChangePwd3Activity.class);
                        intent.putExtra("type", type);
                        intent.putExtra("code", edChangepwd2Code.getText().toString());
                        intent.putExtra("mobile",phone);
                        startActivity(intent);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

        });
    }
}
